package com.cqupt.service.serviceImp;

import com.cqupt.dao.*;
import com.cqupt.paging.Paging;
import com.cqupt.pojo.*;
import com.cqupt.pojo.Package;
import com.cqupt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 蒋俊贤
 * @version 1.0
 */
@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PackageMapper packageMapper;

    @Autowired
    private ChargeOrderMapper   chargeOrderMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsOrderMapper goodsOrderMapper;

    @Autowired
    private AcountMapper  acountMapper;

    @Autowired
    private BetterMapper betterMapper;

    @Autowired
    private PackageOrderMapper packageOrderMapper;

    @Override
    //用户登录
    public User getUserInfo(User user) {
        User tbuser = null;
        UserExample example = new UserExample();
        if(user!=null && user.getUsername()!=null && user.getUserpwd()!=null) {
            example.createCriteria().andUsernameEqualTo(user.getUsername());
            example.createCriteria().andUserpwdEqualTo(user.getUserpwd());
        }else {
            return tbuser;
        }
        List<User> list = userMapper.selectByExample(example);
        if(list.size()>0) {
            tbuser = list.get(0);
        }
        return tbuser;
    }

    @Override
    //注册用户
    //成功返回1，用户名相同返回2，手机号相同返回3
    public int userAdd(User user) {

        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameIsNotNull();
        List<User> list = userMapper.selectByExample(userExample);
        //用户名是否已经存在
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(user.getUsername())){
                return 2;
            }

        }

        //判断电话是否已经注册
        UserExample userExample1 = new UserExample();
        userExample1.createCriteria().andPhoneIsNotNull();
        List<User> list1 = userMapper.selectByExample(userExample1);
        for (int i = 0; i < list1.size(); i++) {
            if(list1.get(i).getPhone().equals(user.getPhone())){
                return 3;
            }
        }
        userMapper.insertSelective(user) ;
        UserExample userExample2 = new UserExample();
        userExample2.createCriteria().andUsernameEqualTo(user.getUsername());
        userExample2.createCriteria().andUserpwdEqualTo(user.getUserpwd());
        List<User> list2 = userMapper.selectByExample(userExample2);
        User user1 = list2.get(0);

        Acount acount = new Acount();
        acount.setNumber(user1.getId());
        acount.setUsername(user.getUsername());
        acount.setUserpwd(user.getUserpwd());
        acount.setPhone(user.getPhone());
        acount.setRoot(0);

        return acountMapper.insertSelective(acount);

    }

    @Override
    //用户修改
    //1表示成功，2表示用户名已经存在 3表示手机号已经注册
    public int userUpdate(User user){
        User tbuser = null;
        UserExample example = new UserExample();
        if(user!=null && user.getId() != null) {
            example.createCriteria().andIdEqualTo(user.getId());
        }
        List<User> list0 = userMapper.selectByExample(example);
        if(list0.size()>0) {
            tbuser = list0.get(0);
        }

        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameIsNotNull();
        List<User> list = userMapper.selectByExample(userExample);
        //用户名是否已经存在
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(user.getUsername())){
                return 2;
            }

        }

        //判断电话是否已经注册
        UserExample userExample1 = new UserExample();
        userExample1.createCriteria().andPhoneIsNotNull();
        List<User> list1 = userMapper.selectByExample(userExample1);
        for (int i = 0; i < list1.size(); i++) {
            if(list1.get(i).getPhone().equals(user.getPhone())){
                return 3;
            }
        }
        if(user.getUsername() != null){
            tbuser.setUsername(user.getUsername());
        }
        if(user.getUserpwd() != null){
            tbuser.setUserpwd(user.getUserpwd());
        }

        if (user.getPhone() != null){
            tbuser.setPhone(user.getPhone());
        }
        userMapper.updateByPrimaryKeySelective(tbuser);
        UserExample userExample2 = new UserExample();
        userExample2.createCriteria().andUsernameEqualTo(tbuser.getUsername());
        userExample2.createCriteria().andUserpwdEqualTo(tbuser.getUserpwd());
        List<User> list2 = userMapper.selectByExample(userExample2);
        User user1 = list2.get(0);

        Acount acount = new Acount();
        acount.setNumber(user1.getId());
        acount.setUsername(user1.getUsername());
        acount.setUserpwd(user1.getUserpwd());
        acount.setPhone(user1.getPhone());
        acount.setRoot(0);
        return acountMapper.insertSelective(acount);

    }

    @Override
    //用户删除
    //1成功 0失败
    public int userDelete(User user){
        User tbuser = null;
        UserExample example = new UserExample();
        if(user!=null && user.getId() != null) {
            example.createCriteria().andIdEqualTo(user.getId());
        }else {
            return 0;
        }

        List<User> list = userMapper.selectByExample(example);
        tbuser = list.get(0);
        AcountExample acountExample = new AcountExample();
        acountExample.createCriteria().andUsernameEqualTo(tbuser.getUsername());
        acountExample.createCriteria().andUserpwdEqualTo(tbuser.getUserpwd());
        acountMapper.deleteByExample(acountExample);
        return userMapper.deleteByExample(example);
    }

    @Override
    //查看套餐类型
    public List getPackageList() {
        return packageMapper.selectByExample(null);
    }

    @Override
    //查看套餐类型分页
    public Paging getPackageList(Integer number, Double price1, Double price2) {
        PackageExample packageExample = new PackageExample();
        Paging paging = new Paging();
        if(price1 == null && price2 == null){
            packageExample.createCriteria().andIdIsNotNull();
        }else{
            packageExample.createCriteria().andPriceBetween(price1,price2);
        }
        List<Package> packages = packageMapper.selectByExample(packageExample);
        if(packages.size() <= 6){
            paging.setList(packages);
            paging.setNumber(packages.size());
            return paging;
        }
        ArrayList<Package> packagesLimit = new ArrayList<>();
        if(packages.size() <= 6*number){
            for (int i = 6*(number - 1); i < packages.size(); i++) {
                packagesLimit.add(packages.get(i));
            }
            paging.setList(packagesLimit);
            paging.setNumber(packages.size());
            return paging;

        }

        for (int i = 6*(number - 1); i < 6 * number; i++) {
            packagesLimit.add(packages.get(i));
        }
        paging.setList(packagesLimit);
        paging.setNumber(packages.size());
        return paging;



    }

    @Override
    //展示少量精品
    public List getBetter(){
        return betterMapper.selectByExample(null);
    }

    @Override
    //查看用户余额
    public double getUserMoney(User user) {
        User tbuser = null;
        UserExample example = new UserExample();
        if(user!=null && user.getId() != null) {
            example.createCriteria().andIdEqualTo(user.getId());
        }
        List<User> list = userMapper.selectByExample(example);
        if(list.size()>0) {
            tbuser = list.get(0);
        }
        return tbuser.getMoney();

    }

    @Override
    //用户充值，添加充值表信息，改变用户余额
    //用户为空，或id为空返回0
    //成功返回1
    public int charge(User user ,Double money2) {

        User tbuser = null;
        UserExample example = new UserExample();
        if(user!=null && user.getId() != null) {
            example.createCriteria().andIdEqualTo(user.getId());
        }else {
            return 0;
        }
        List<User> list = userMapper.selectByExample(example);
        if(list.size()>0) {
            tbuser = list.get(0);
        }
        tbuser.setMoney(tbuser.getMoney() + money2);
        tbuser.setScore(tbuser.getScore() + (int)money2.doubleValue());
        userMapper.updateByPrimaryKeySelective(tbuser);
        ChargeOrder chargeOrder = new ChargeOrder();
        chargeOrder.setUsername(tbuser.getUsername());
        chargeOrder.setPhone(tbuser.getPhone());
        chargeOrder.setMoney(money2);
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        String data = dateFormat.format(new Date());
        chargeOrder.setTime(data);
        chargeOrder.setSocre((int)money2.doubleValue());
        return chargeOrderMapper.insertSelective(chargeOrder);
    }

    @Override
    //获取商品表
    public List getGoodsList() {
        return goodsMapper.selectByExample(null);
    }

    @Override
    //获取商品表分页
    public Paging getGoodsList(Integer number,Integer score1,Integer score2){
        GoodsExample goodsExample = new GoodsExample();
        Paging paging = new Paging();
        if(score1 == null || score2 ==null) {
            goodsExample.createCriteria().andIdIsNotNull();
        }else{
            goodsExample.createCriteria().andScoreBetween(score1,score2);
        }
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        if(goods.size() <= 6){
            paging.setList(goods);
            paging.setNumber(goods.size());
            return paging;
        }
        ArrayList<Goods> goodsLimit = new ArrayList<>();
        if(goods.size() <= 6 * number) {
            for (int i = 6 * (number - 1); i < goods.size(); i++) {
                goodsLimit.add(goods.get(i));
            }
            paging.setList(goodsLimit);
            paging.setNumber(goods.size());
            return paging;

        }

        for (int i = 6 * (number - 1); i < 6 * number; i++) {
            goodsLimit.add(goods.get(i));
        }
        paging.setList(goodsLimit);
        paging.setNumber(goods.size());
        return paging;


    }



    @Override
    //用户用积分购买商品，用户积分减少，商品订单表更新，商品库存减少
    //积分不够返回2，库存不够返回3，商品已经下架返回4
    //用户或商品id为空返还0
    //成功返还1
    public int shopping(Integer userid,Integer id,Integer number){
        User tbuser = null;
        UserExample example = new UserExample();
        example.createCriteria().andIdEqualTo(userid);
        List<User> list = userMapper.selectByExample(example);
        if(list.size()>0) {
            tbuser = list.get(0);
        }

        Goods tbgoods = null;
        GoodsExample example2 = new GoodsExample();
        example2.createCriteria().andIdEqualTo(id);
        List<Goods> list2 = goodsMapper.selectByExample(example2);
        if(list2.size()>0) {
            tbgoods = list2.get(0);
        }
//        //积分不够
//        if(tbuser.getScore()< tbgoods.getScore() * number){
//            return 2;
//        }
//        //库存不够
//        if(tbgoods.getNumber() < number){
//            return 3;
//        }
//
//        //商品已经下架
//        if(tbgoods.getState() == 0){
//            return 4;
//        }

        //用户积分减少
        tbuser.setScore(tbuser.getScore() - tbgoods.getScore() * number);
        userMapper.updateByPrimaryKeySelective(tbuser);
        //商品订单表更新
        GoodsOrder goodsOrder = new GoodsOrder();
        goodsOrder.setUsername(tbuser.getUsername());
        goodsOrder.setPhone(tbuser.getPhone());
        goodsOrder.setScore(tbgoods.getScore() * number);
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        String date = dateFormat.format(new Date());
        goodsOrder.setTime(date);
        goodsOrder.setNumbers(number);
        goodsOrder.setGoodsname(tbgoods.getName());

        int  num = (int)(Math.random()*3);
        goodsOrder.setState(num);
        goodsOrderMapper.insertSelective(goodsOrder);

        //商品库存减少
        tbgoods.setNumber(tbgoods.getNumber() - number);

        //如果商品卖完了就下架
        if(tbgoods.getNumber() == 0){
            tbgoods.setState(0);
        }
        goodsMapper.updateByPrimaryKeySelective(tbgoods);


        return 1;
    }

    @Override
    //用户用余额购买套餐，用户余额减少，积分增加，套餐订单表更新，
    //余额不足返回2,商品下架返回3
    //用户或套餐id为空返回0;
    //成功返回1
    public int subscribe(Integer userid,Integer id){
        User tbuser = null;
        Package tbpackage = new Package();
        UserExample example = new UserExample();
        example.createCriteria().andIdEqualTo(userid);
        List<User> list = userMapper.selectByExample(example);
        if(list.size()>0) {
            tbuser = list.get(0);
        }
        PackageExample packageExample = new PackageExample();
        packageExample.createCriteria().andIdEqualTo(id);
        List<Package> packages = packageMapper.selectByExample(packageExample);
        if(packages.size()>0){
            tbpackage = packages.get(0);
        }

//        //余额不足 返回2
//        if(tbuser.getMoney() < tbpackage.getPrice()){
//            return 2;
//        }
//
//        //商品下架返回3
//        if(tbpackage.getState() == 0){
//            return 3;
//        }

        //用户余额减少
        tbuser.setMoney(tbuser.getMoney() - tbpackage.getPrice());

        //用户积分增加
        tbuser.setScore(tbuser.getScore() + tbpackage.getScore());
        userMapper.updateByPrimaryKeySelective(tbuser);

        //套餐订单表更新
        PackageOrder packageOrder = new PackageOrder();
        packageOrder.setUsername(tbuser.getUsername());
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        String date = dateFormat.format(new Date());
        packageOrder.setTime(date);
        packageOrder.setPhone(tbuser.getPhone());
        packageOrder.setPrice(tbpackage.getPrice());
        packageOrder .setSocre(tbpackage.getScore());
        packageOrder.setPackagename(tbpackage.getName());
        packageOrderMapper.insertSelective(packageOrder);
        return 1;

    }

    @Override
    //获取商品订单表
    public List getUserOrderList() {
        return goodsOrderMapper.selectByExample(null);
    }

    @Override
    //获取商品订单表分页
    public Paging getUserOrderList(Integer number) {
        GoodsOrderExample goodsOrderExample = new GoodsOrderExample();
        goodsOrderExample.createCriteria().andIdBetween(8*(number - 1) + 1,8*number);
        List<GoodsOrder> goodsOrders = goodsOrderMapper.selectByExample(goodsOrderExample);

        GoodsOrderExample goodsOrderExample1 = new GoodsOrderExample();
        goodsOrderExample1.createCriteria().andIdIsNotNull();
        List<GoodsOrder> goodsOrders1 = goodsOrderMapper.selectByExample(goodsOrderExample1);
        Paging paging = new Paging();
        paging.setList(goodsOrders);
        paging.setNumber(goodsOrders1.size());
        return paging;
    }


    //获取商品订单表分页的模糊查询
    public Paging getUserOrderLike(Integer number,String fuzzy){
        Paging paging = new Paging();
        GoodsOrderExample goodsOrderExample = new GoodsOrderExample();
        goodsOrderExample.createCriteria().andTimeLike("%"+fuzzy+"%");
        List<GoodsOrder> goodsOrders = goodsOrderMapper.selectByExample(goodsOrderExample);

        if(goodsOrders.size() <= 8){
            paging.setList(goodsOrders);
            paging.setNumber(goodsOrders.size());
            return paging;
        }

        List<GoodsOrder> goodsOrdersLimit = new ArrayList<>();
        if(goodsOrders.size() <= 8 * number) {
            for (int i = 8 * (number - 1); i < goodsOrders.size(); i++) {
                goodsOrdersLimit.add(goodsOrders.get(i));
            }
            paging.setList(goodsOrdersLimit);
            paging.setNumber(goodsOrders.size());
            return paging;

        }

        for (int i = 8 * (number - 1); i < 8 * number; i++) {
            goodsOrdersLimit.add(goodsOrders.get(i));
        }
        paging.setList(goodsOrdersLimit);
        paging.setNumber(goodsOrders.size());
        return paging;

    }

    @Override
    //获取套餐订单表
    public List getPackageOrder(){
        return packageOrderMapper.selectByExample(null);
    }

    @Override
    //获取套餐订单表分页
    public Paging getPackageOrder(Integer number){
        PackageOrderExample packageOrderExample = new PackageOrderExample();
        packageOrderExample.createCriteria().andIdBetween(8*(number - 1) + 1,8*number);
        List<PackageOrder> packageOrders = packageOrderMapper.selectByExample(packageOrderExample);

        PackageOrderExample packageOrderExample1 = new PackageOrderExample();
        packageOrderExample1.createCriteria().andIdIsNotNull();
        List<PackageOrder> packageOrders1 = packageOrderMapper.selectByExample(packageOrderExample1);
        Paging paging = new Paging();
        paging.setList(packageOrders);
        paging.setNumber(packageOrders1.size());
        return paging;

    }

    //获取套餐订单表分页的模糊查询
    public Paging getPackageLike(Integer number,String fuzzy){
        Paging paging = new Paging();
        PackageOrderExample packageOrderExample = new PackageOrderExample();
        packageOrderExample.createCriteria().andTimeLike("%" + fuzzy + "%");
        List<PackageOrder> packageOrders = packageOrderMapper.selectByExample(packageOrderExample);
        if(packageOrders.size() <= 8){
            paging.setList(packageOrders);
            paging.setNumber(packageOrders.size());
            return  paging;
        }
        List<PackageOrder> packageOrdersLimit = new ArrayList<>();
        if(packageOrders.size() <= 8*number){
            for (int i = 8 * (number - 1); i < packageOrders.size(); i++) {
                packageOrdersLimit.add(packageOrders.get(i));
            }
            paging.setList(packageOrdersLimit);
            paging.setNumber(packageOrders.size());
            return paging;
        }
        for (int i = 8 * (number - 1); i < 8 * number; i++) {
            packageOrdersLimit.add(packageOrders.get(i));
        }
        paging.setList(packageOrdersLimit);
        paging.setNumber(packageOrders.size());
        return paging;

    }

    @Override
    //积分查询
    public int getScore(Integer number){
        return userMapper.selectByPrimaryKey(number).getScore();
    }


}
